package collection;

import org.junit.Test;

import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author: feiwang_6
 * @create: 2020/4/8 10:09
 * @description:
 */
public class CollectionTest {
    
    public static void main(String[] args) {
        int[] numbers = new int[]{4, 2, 1, 6, 9, 10, 8};
        System.out.println(sortArray(numbers, 0, numbers.length-1));
    }
    
    
    public static int[] sortArray(int[] numbers, int min, int max){
        if(min + 1 >= max){
            if(numbers[min] > numbers[max]){
                int temp = numbers[min];
                numbers[min] = numbers[max];
                numbers[max] = temp;
                return numbers;
            }
        }
        int mid = (min + max)/2;
        numbers = sortArray(numbers, min, mid);
        int[] ints = sortArray(numbers, mid, max);
        return ints;
    }
    
    
    
    @Test
    public void testSum(){
        Random random1 = new Random(1000);
        for(int i = 0; i < 20; i++){
            System.out.print(random1.nextInt(100));
            System.out.print(",");
        }
        System.out.println();
        Random random2 = new Random(10);
        for(int i = 0; i < 20; i++){
            System.out.print(random2.nextInt(100));
            System.out.print(",");
        }
        System.out.println();
        Random random3 = new Random(10);
        for(int i = 0; i < 20; i++){
            System.out.print(random3.nextInt(100));
            System.out.print(",");
        }
    }
    
    public int threeSumOfTarget(int[] nums, int target){
        return 0;
    }

    public List<List<Integer>> threeSum(int[] nums){
        List<List<Integer>> result = new ArrayList<>();
        if(nums.length < 3){
            return result;
        }
        Arrays.sort(nums);
        for (int i = 0; i < nums.length; i++){
            if(nums[i] > 0){
                return result;
            }
            if(i > 0 && nums[i] == nums[i-1]){
                continue;
            }
            
            int l = i + 1;
            int r = nums.length-1;
            int temp = 0;
            while (l < r){
                temp = nums[i] + nums[l] + nums[r];
                if(temp == 0){
                    ArrayList<Integer> list = new ArrayList<>();
                    list.add(nums[i]);
                    list.add(nums[l]);
                    list.add(nums[r]);
                    result.add(list);
                    //左指针相等
                    while (l < r && nums[l] == nums[l+1]){
                        l++;
                    }
                    //右指针相等
                    while(l < r && nums[r] == nums[r - 1]){
                        r--;
                    }
                    l++;
                    r--;
                }else if (temp < 0){
                    l++;
                }else {
                    r--;
                }
            }
        }
        return result;
    }
    
    @Test
    public void testList(){
        ArrayList<Integer> list = new ArrayList<>();
        list.add(199);
        
    }
    
    @Test
    public void testVector(){
        Vector<Integer> vector = new Vector<>();
        vector.add(10);
        vector.add(20);
        vector.add(1);
        vector.add(100);
        vector.addElement(100);
        System.out.println(vector);
        vector.sort(Integer::compareTo);
        System.out.println(vector);
        vector.remove(1);
        vector.removeElement(1);
        
        ArrayList<Integer> list = new ArrayList();
        list.remove(1);
        
    }
    
    @Test
    public void testTreeSet(){
        TreeSet<Integer> set = new TreeSet();
        set.add(1);
    }
    @Test
    public void testTreeMap(){
        TreeMap<Integer, Integer> map = new TreeMap();
        map.put(60, 60);
        map.put(56, 56);
        map.put(68, 68);
        map.put(45, 45);
        System.out.println(map.get(60));
        map.remove(60);
    }
    
    @Test
    public void testLinkedList(){
        LinkedList<Integer> list = new LinkedList();
        for (int i = 0; i < 5000; i++){
            list.add(i);
        }
        listByNormalFor(list);
        ListByStrengThenFor(list);
        listByIterator(list);
    }

    /**
     * 通过快速随机访问遍历LinkedList
     */
    private void listByNormalFor(LinkedList<Integer> list) {
        // 记录开始时间
        long start = System.currentTimeMillis();
        int size = list.size();
        for (int i = 0; i < size; i++) {
            list.get(i);
        }
        // 记录用时
        long interval = System.currentTimeMillis() - start;
        System.out.println("listByNormalFor：" + interval + " ms");
    }

    /**
     * 通过增强for循环遍历LinkedList
     * @param list
     */
    public static void ListByStrengThenFor(LinkedList<Integer> list){
        // 记录开始时间review
        long start = System.currentTimeMillis();
        for (Integer i : list) { }
        // 记录用时
        long interval = System.currentTimeMillis() - start;
        System.out.println("ListByStrengThenFor：" + interval + " ms");
    }

    /**
     * 通过快迭代器遍历LinkedList
     */
    private static void listByIterator(LinkedList<Integer> list) {
        // 记录开始时间
        long start = System.currentTimeMillis();
        for(Iterator iter = list.iterator(); iter.hasNext();) {
            iter.next();
        }
        // 记录用时
        long interval = System.currentTimeMillis() - start;
        System.out.println("listByIterator：" + interval + " ms");
    }
    
    
    @Test
    public void testLinkedHashMap(){
        LinkedHashMap<Integer, String> map = new LinkedHashMap<>();
        map.put(10, "100");
    }

    @Test
    public void testHashMap() throws InterruptedException {
        HashMap<Integer, Integer> map = new HashMap<>(1);
        AtomicInteger aInteger = new AtomicInteger();
        
        Runnable runnable = () -> {
            while (aInteger.get() < 10000){
                map.put(aInteger.get(), aInteger.get());
                aInteger.incrementAndGet();
            }
        };
        
        List<Thread> list = new ArrayList<>();
        for(int i = 0; i < 10; i++){
            Thread thread = new Thread(runnable);
            thread.start();
            list.add(thread);
        }
        
        for(Thread t : list){
            t.join();
        }

        System.out.println(map.size());
        int count=0;
        for(int i=0; i<10000; i++){
            if(!map.containsKey(i)){
                count++;
            }
        }
        System.out.println(map.size());
        System.out.println("不存在：" + count);
    }
    
    @Test
    public void testHashTable(){
        Hashtable<Integer, Integer> map = new Hashtable();
        map.put(26, 26);
        map.put(24, 24);
        map.put(15, 15);
        map.put(2, 2);
        map.put(13, 13);
        System.out.println(map.get(13));
    }
}
